import datetime
from rqdatac import *
import rqdatac as rq
import backtrader as bt
import pandas as pd

class myStrategy(bt.Strategy):
    pass

# 使用米筐获取螺纹钢主连合约日线行情数据
# rq.init()
# RB_data=rq.futures.get_dominant_price(underlying_symbols='RB', start_date=20200101, end_date=20250630, frequency='1d', fields=None, adjust_type='none', adjust_method='prev_close_spread')
# df=pd.DataFrame(RB_data)
# df.to_csv('RB_data.csv')

# 1.create a cerebro
cerebro = bt.Cerebro()

# 2.1.1 Create a datafeed using pandas
# dataframe=pd.read_csv('RB_data.csv')
# dataframe['date']=pd.to_datetime(dataframe['date'])
# dataframe.set_index('date',inplace=True)
# brf_daily=bt.feeds.PandasData(dataname=dataframe,
#                               fromdate=datetime.datetime(2020,1,1),
#                               todate=datetime.datetime(2025,6,30))

# 2.1.2 Create a CSV datafeed
brf_daily=bt.feeds.GenericCSVData(dataname='Strategy_Pass/RB_data3.csv',
                                  fromdate=datetime.datetime(2020,1,1),
                                  todate=datetime.datetime(2025,6,30),
                                  null_value=0,
                                  dtformat='%Y-%m-%d',
                                  date=0,
                                  open=3,
                                  close=4,
                                  high=5,
                                  low=6,
                                  volume=8,
                                  open_interest=12
                                  )

# 2.2add the data feed to Cerebro
cerebro.adddata(brf_daily)

# 3.add my strategy to cerebro
cerebro.addstrategy(myStrategy)

# 4.run
cerebro.run()

# 5. plot the result
cerebro.plot(style='candle')
